package com.ycsj.goldmedalnewconcept.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;





import java.util.ArrayList;
import java.util.List;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.adapter.MusicAdapter;
import com.ycsj.goldmedalnewconcept.bean.MusicInfo;

/**
 * 创建日期：2017/11/10  18:24
 * 描述:小组音乐界面
 * 作者:张伟
 */

public class TeamMusicActivity extends Activity implements View.OnClickListener {

    private LinearLayout llBack;

    private ListView lv;

    private List<MusicInfo> infos;
    private MusicAdapter adapter;

    //播放音乐的资源集合
    private int[] musicId = {R.raw.music1, R.raw.music2, R.raw.music3, R.raw.music4, R.raw.music5,R.raw.music6,R.raw.music7,R.raw.music8,R.raw.music9,R.raw.music10};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_music);

        //设置状态栏
        setBar();

        //初始化View
        initView();

        //设置监听
        initListener();

        //创建数据集合
        initData();

        //设置适配器
        setLv();

    }


    /**
     * 创建本地的播放音乐的数据集合
     */
    private void initData() {
        infos = new ArrayList<>();
        for (int i = 1; i <= musicId.length; i++) {
            MusicInfo info = new MusicInfo();
            info.setName("music_" + i + ".mp3");
            info.setResId(musicId[i - 1]);
            info.setPlaying(false);
            infos.add(info);
        }
    }


    /**
     * 设置适配器的方法
     */
    private void setLv() {
        adapter = new MusicAdapter(this, infos, R.layout.item_lv_music);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
                //条目的点击事件
                Intent intent = new Intent();
                intent.putExtra("index", pos + 1);
                setResult(0x110, intent);
                finish();
            }
        });
    }

    /**
     * 初始化控件
     */
    private void initView() {
        llBack = (LinearLayout) findViewById(R.id.ll_back);
        lv = (ListView) findViewById(R.id.lv);
    }


    /**
     * 设置监听的方法
     */
    private void initListener() {
        llBack.setOnClickListener(this);
    }


    /**
     * 页面控件的点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }


    /**
     * 设置沉浸状态栏的方法,需要在setContentView()之后调用
     */
    @SuppressLint("NewApi")
	private void setBar() {
        //兼容5.0+，状态栏设置透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else
            //为了沉浸式状态栏 4.4以上才有
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
    }

}
